package file.read.csvAdnVcard;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import a_vcard.android.syncml.pim.PropertyNode;
import a_vcard.android.syncml.pim.VDataBuilder;
import a_vcard.android.syncml.pim.VNode;
import a_vcard.android.syncml.pim.vcard.VCardException;
import a_vcard.android.syncml.pim.vcard.VCardParser;

public class ReadVcard {

	public static void main(String[] args) throws Exception {

		VCardParser parser = new VCardParser();
		VDataBuilder builder = new VDataBuilder();

		String file = "E:/联系人导出/gmailnew/contacts.vcf";

		// read whole file to string
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				new FileInputStream(file), "UTF-8"));

		String vcardString = "";
		String line;
		while ((line = reader.readLine()) != null) {
			vcardString += line + "\n";
		}
		reader.close();

		// parse the string
		boolean parsed = parser.parse(vcardString, "UTF-8", builder);
		if (!parsed) {
			throw new VCardException("Could not parse vCard file: " + file);
		}

		// get all parsed contacts
		List<VNode> pimContacts = builder.vNodeList;

		// do something for all the contacts
		for (VNode contact : pimContacts) {
			ArrayList<PropertyNode> props = contact.propList;
			System.out.println(props);
			// contact name - FN property
			String name = null;
			for (PropertyNode prop : props) {
				if ("FN".equals(prop.propName)) {
					name = prop.propValue;
					break;
				}
			}

			System.out.println("Found contact: " + name);
		}

	}
}
